package com.softwaredesign.springboot.controller;

import com.softwaredesign.springboot.controller.dto.RoleAuthorityDTO;
import com.softwaredesign.springboot.entity.RoleAuthority;
import com.softwaredesign.springboot.mapper.RoleAuthorityMapper;
import com.softwaredesign.springboot.service.RoleAuthorityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/roleAuthority")
public class RoleAuthorityController {

    @Autowired
    private RoleAuthorityService roleAuthorityService;

    @Autowired
    private RoleAuthorityMapper roleAuthorityMapper;

    @GetMapping("/all")
    public Map<String, Object> findAll() {
        List<RoleAuthority> data = roleAuthorityService.findAll();
        Integer total = data.size();
        Map<String, Object> res = new HashMap<>();
        res.put("data", data);
        res.put("total", total);
        return res;
    }

    @PostMapping("/save")
    public int save(@RequestBody RoleAuthorityDTO roleAuthorityDTO) {
        return roleAuthorityService.save(roleAuthorityDTO);
    }

    @DeleteMapping("/{id1}/{id2}")
    public Integer delete(@PathVariable Integer id1, @PathVariable Integer id2) {
        return roleAuthorityMapper.deleteById(id1, id2);
    }

    @GetMapping("/roleID")
    public Map<String, Object> findPage(@RequestParam Integer roleID) {
        List<RoleAuthority> data = roleAuthorityService.selectPage(roleID);
        Integer total = data.size();
        Map<String, Object> res = new HashMap<>();
        res.put("data", data);
        res.put("total", total);
        return res;
    }

}
